import 'package:flutter/material.dart';
import 'package:flutter_ohos/components/MTFoodItem.dart';

class MTBody extends StatefulWidget {
  MTBody({Key? key, required this.list}) : super(key: key);

  final List list;

  @override
  _MTBodyState createState() => _MTBodyState();
}

class _MTBodyState extends State<MTBody> {
  int _activeIndex = 0;

  int getChildCount() {
    if (widget.list.length > 0) {
      return widget.list[_activeIndex]["foods"].length;
    }
    return 0;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromRGBO(255, 245, 230, 1),
      width: double.infinity,
      height: double.infinity,
      child: Row(
        children: [
          Container(
            width: 120, // ListView必须放在有宽度的容器中
            color: Color.fromARGB(255, 209, 206, 206),
            child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: () {
                    setState(() {
                      _activeIndex = index;
                    });
                  },
                  child: Container(
                    color: _activeIndex == index
                        ? Colors.white
                        : const Color.fromARGB(255, 209, 206, 206),
                    height: 80,
                    alignment: Alignment.center,
                    child: Text(
                      widget.list[index]["name"],
                      style: TextStyle(fontSize: 16, color: Colors.black),
                    ),
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  // width: double.infinity,
                  height: 1,
                  color: Colors.white,
                );
              },
              itemCount: widget.list.length,
            ),
          ),
          Expanded(
              child: ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return MTFoodItem(
                      foodItem: widget.list[_activeIndex]["foods"][index],
                    );
                  },
                  itemCount: getChildCount()))
        ],
      ),
    );
  }
}
